//
//  JokeCollectionViewCell.m
//  UI_Project_A
//
//  Created by dllo on 16/1/30.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "JokeCollectionViewCell.h"
#import "JokeTableViewCell.h"
#import "HandleData.h"
#import "ModelOfJoke.h"
#import <DKNightVersion.h>
#import <MJRefresh.h>


@interface JokeCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)NSMutableArray *arrayCount;
@property (nonatomic ,assign)int number;

@end

@implementation JokeCollectionViewCell



- (void)dealloc {

    [_tableview release];
    [super dealloc];
}



- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        self.number = 0;
        [self data];
        [self createTableview];
        [self handleRefresh];
    }


    return self;
}


- (void)data {
    self.arrayCount = [NSMutableArray array];
    
//    NSLog(@"%d",self.number);
    
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/expert/%d-10.html",self.number];
    NSLog(@"%@",str);
    [HandleData handleFloewNewWithString:str Block:^(id result) {
        
     NSDictionary *dic = [result objectForKey:@"data"];
        
        NSDictionary *dicexpertList = [dic objectForKey:@"expertList"];
        for (NSDictionary *dicData in dicexpertList) {
            ModelOfJoke *model = [[ModelOfJoke alloc] init];
           model.name = [dicData objectForKey:@"name"];
            model.des = [dicData objectForKey:@"description"];
            model.headpicurl = [dicData objectForKey:@"headpicurl"];
            model.picurl = [dicData objectForKey: @"picurl"];
            model.alias = [dicData objectForKey:@"alias"];
            model.classification =[dicData objectForKey:@"classification"];
            model.concernCount = [dicData objectForKey:@"concernCount"];
            model.questionCount = [dicData objectForKey:@"questionCount"];
            model.title = [dicData objectForKey:@"title"];
            model.expertId = [dicData objectForKey:@"expertId"];
            
            [self.arrayCount addObject:model];
            [model release];
            
        }
        [self.tableview reloadData];
    }];

    self.number = self.number + 10;


}



- (void)handleRefresh {
    
//    
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.number = self.number - 20;
        if (self.number >= 0) {
            NSLog(@"%d",self.number);
        [self data];
        [self.tableview.mj_header endRefreshing];
        [self.tableview reloadData];
        }else{
            [self.tableview.mj_header endRefreshing];
            [self.tableview reloadData];

        
        }
    }];
//
    
    

    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        if (self.number <= 40) {
            
        [self data];
        [self.tableview.mj_footer endRefreshing];
        [self.tableview reloadData];
        }else {
        
            [self.tableview.mj_footer endRefreshing];
            [self.tableview reloadData];
        }
    }];

}



- (void)createTableview {



    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableview];
    [self.tableview release];

    self.tableview.separatorStyle = UITableViewCellSelectionStyleNone;
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;

    [self.tableview registerClass:[JokeTableViewCell class] forCellReuseIdentifier:@"pool"];

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayCount.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


  JokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
 ModelOfJoke *model = [self.arrayCount objectAtIndex:indexPath.row];
    
    [cell passModelWith:model];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    return self.frame.size.height /7 * 4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {

    ModelOfJoke *model = [self.arrayCount objectAtIndex:indexPath.row];
    
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"questiontitle" object:nil userInfo:@{@"content":model}];

}



@end
